package com.springboot.controller;

import com.springboot.constant.CommonConstant;
import com.springboot.domain.BtDemand;
import com.springboot.domain.BtPerson;
import com.springboot.domain.BtSkills;
import com.springboot.domain.Result;
import com.springboot.dto.PersonDetailDTO;
import com.springboot.dto.PersonModifyDTO;
import com.springboot.enums.ResultEnum;
import com.springboot.jpa.BankJPA;
import com.springboot.repository.DemandRepository;
import com.springboot.repository.PersonRepository;
import com.springboot.repository.SkillsRepository;
import com.springboot.service.PersonalService;
import com.springboot.tools.ResultUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by Administrator on 2017/7/11.
 */
@RequestMapping(value = "/bank")
@RestController
public class PersonController {
    private PersonalService personalService;
    private PersonRepository personRepository;
    private DemandRepository demandRepository;
    private SkillsRepository skillsRepository;
    private BankJPA bankJPA;
    @Autowired
    public PersonController(PersonalService personalService, DemandRepository demandRepository, PersonRepository personRepository, SkillsRepository skillsRepository,BankJPA bankJPA) {
        this.personalService = personalService;
        this.demandRepository = demandRepository;
        this.personRepository = personRepository;
        this.demandRepository = demandRepository;
        this.personRepository = personRepository;
        this.skillsRepository = skillsRepository;
        this.bankJPA=bankJPA;
    }

    //完善修改个人信息
    @ResponseBody
    @PostMapping(value = "/person/modifyPerson")
    public Result modifyPerson(@Valid @RequestBody PersonModifyDTO personModifyDTO, BindingResult bindingResult, HttpSession session) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            for (ObjectError error : errorList) {
                return ResultUtil.error(error.getDefaultMessage());
            }
        }
        personalService.updatePersonById(personModifyDTO, session);
        return ResultUtil.success("success");
    }

    //个人信息详情
    @ResponseBody
    @GetMapping(value = "/person/personInfoDetail")
    public Result personDetail(HttpSession session) {
        String id = (String) session.getAttribute("user");
        if (null == id) {
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
        return personalService.findPersonDetails(id);
    }

    //个人信息详情（其他人查看）
    @ResponseBody
    @GetMapping(value = "/person/findPersonDetailById/{id}")
    public Result findPersonDetailById(@PathVariable String id) {
        return personalService.findPersonDetailById(id);
    }

    @ResponseBody
    @GetMapping(value = "/demand/signed/list")
    public Result signedDemandList(@ApiParam Pageable pageable, HttpSession session) {
        String userId = (String) session.getAttribute("user");
        if (null == userId) {
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
        Page<BtDemand> pages = demandRepository.findSignedListByUser(userId, pageable);
        return ResultUtil.success(pages);
    }

    //个人/企业需求列表
    @ResponseBody
    @GetMapping(value = "/demand/personEnterDemandList")
    public Result personEnterDemandList(@ApiParam Pageable pageable, HttpSession session) {
        Page<BtDemand> demands = personalService.demandList(pageable, session);
        return ResultUtil.success(demands);
    }

    //个人通过审核的列表
    @ResponseBody
    @GetMapping(value = "/person/personList")
    public Result personList(@ApiParam Pageable pageable) {
        return ResultUtil.success(bankJPA.getPersonList(CommonConstant.PASS, pageable));
    }

    /**
     * skill list
     *
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/person/skills")
    public Result skillList() {
        List<BtSkills> skills = skillsRepository.findAll();
        return ResultUtil.success(skills);
    }
}
